package cn.stylefeng.guns.modular.rest.make;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicDriverParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPrivatecarParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicDriverResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPrivatecarResult;
import cn.stylefeng.guns.modular.basic.service.BasicDriverService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicPrivatecarService;
import cn.stylefeng.guns.modular.make.entity.MakeRailwayPlatformManage;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformManageParam;
import cn.stylefeng.guns.modular.make.model.params.MakeRailwayPlatformParam;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformManageResult;
import cn.stylefeng.guns.modular.make.model.result.MakeRailwayPlatformResult;
import cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformManageService;
import cn.stylefeng.guns.modular.make.service.MakeRailwayPlatformService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;

/**
 *月台预约控制器
 */
@Controller
@RequestMapping("/rest/railwayPlatform")
public class restRailwayPlatformsController extends BaseController {

    @Autowired
    private UserAop userAop;

    @Autowired
    private MakeRailwayPlatformService makeRailwayPlatformService;

    @Autowired
    private MakeRailwayPlatformManageService makeRailwayPlatformManageService;

    @Autowired
    private BasicPrivatecarService basicPrivatecarService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 查询月台列表
     */
    @ResponseBody
    @RequestMapping("/getRailwayPlatformList")
    public ResponseData getRailwayPlatformList(Long operateId,MakeRailwayPlatformParam param) {
    	if(operateId != null) {
    		BasicOperate operate = basicOperateService.getById(operateId);
    		param.setDeptId(operate.getDeptId());
    		return ResponseData.success(makeRailwayPlatformService.findListBySpec(param,operateId));
    	} else {
    		return ResponseData.success();
    	}
    }

    @Autowired
    private BasicDriverService basicDriverService;
    
    /**
     * 查询司机个人信息
     */
    @ResponseBody
    @RequestMapping("/getCar")
    public ResponseData getCar(BasicDriverParam basicDriverParam) {
    	User user = userAop.getUser();
    	basicDriverParam.setUserId(user.getUserId());
    	BasicDriverResult basicDriverResult = basicDriverService.findBySpec(basicDriverParam,null);
    	BasicPrivatecarResult basicPrivatecarResult = new BasicPrivatecarResult();
    	if(basicDriverResult != null) {
    		BasicPrivatecarParam BasicPrivatecarParam = new BasicPrivatecarParam();
    		BasicPrivatecarParam.setDriverId1(Long.parseLong(basicDriverResult.getId().toString()));
    		basicPrivatecarResult = basicPrivatecarService.findBySpec(BasicPrivatecarParam, null,null);
    	}
		return ResponseData.success(basicPrivatecarResult);
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/getById")
    @ResponseBody
    public ResponseData detail(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
        MakeRailwayPlatformManage detail = this.makeRailwayPlatformManageService.getById(makeRailwayPlatformManageParam.getId());
        return ResponseData.success(detail);
    }
    
    /**
     * 新增预约  月台
     */
    @ResponseBody
    @RequestMapping("/addItem")
    public ResponseData addItem(MakeRailwayPlatformManageParam param,Long operateId) {
    	User user = userAop.getUser();
		BasicOperate operate = basicOperateService.getById(operateId);
    	return makeRailwayPlatformManageService.addMake(param,user,operate);
    }

    /**
     * 查询我的预约 列表
     */
    @ResponseBody
    @RequestMapping("/mylist")
    public LayuiPageInfo mylist(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam,String condition ,Long operateId) {
    	if(operateId != null) {
    		BasicOperate operate = basicOperateService.getById(operateId);
    		makeRailwayPlatformManageParam.setDeptId(operate.getDeptId());
    	}
        return this.makeRailwayPlatformManageService.findMyPageBySpec(makeRailwayPlatformManageParam,condition,operateId);
    }
    
    /**
     * 查询已预约 列表
     */
    @ResponseBody
    @RequestMapping("/yilist")
    public ResponseData yilist(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam,Long operateId) {
    	MakeRailwayPlatformParam makeRailwayPlatformParam = new MakeRailwayPlatformParam();
    	if(operateId != null) {
    		BasicOperate operate = basicOperateService.getById(operateId);
    		makeRailwayPlatformParam.setDeptId(operate.getDeptId());
    	}
    	List<MakeRailwayPlatformResult> list = makeRailwayPlatformService.findListBySpec(makeRailwayPlatformParam,operateId);
    	for (MakeRailwayPlatformResult r : list) {
    		makeRailwayPlatformManageParam.setRailwayPlatformId(Long.parseLong(r.getId().toString()));
    		List<MakeRailwayPlatformManageResult> list2 = makeRailwayPlatformManageService.findListBySpec(makeRailwayPlatformManageParam);
    		if(list2.size()==0) {
    			r.setState("空闲");
    		} else {
    			r.setList(list2);
    		}
		}
    	return ResponseData.success(list);
    }
    
    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	User user = userAop.getUser();
    	return this.makeRailwayPlatformManageService.editMake(makeRailwayPlatformManageParam,user);
    }
    
    /**
     * 更改状态接口
     */
    @RequestMapping("/editstate")
    @ResponseBody
    public ResponseData editstate(MakeRailwayPlatformManageParam makeRailwayPlatformManageParam) {
    	if(makeRailwayPlatformManageParam.getState().equals("进行中")) {
    		MakeRailwayPlatformManage makeRailwayPlatformManage = makeRailwayPlatformManageService.getById(makeRailwayPlatformManageParam.getId());
    		
    		MakeRailwayPlatformManageParam MakeRailwayPlatformManageParamyw = new MakeRailwayPlatformManageParam();
    		MakeRailwayPlatformManageParamyw.setRailwayPlatformId(makeRailwayPlatformManage.getRailwayPlatformId());
    		MakeRailwayPlatformManageParamyw.setState("进行中");
    		MakeRailwayPlatformManageParamyw.setArriveTime(makeRailwayPlatformManage.getArriveTime().substring(0, 10));
    		MakeRailwayPlatformManageResult manageResultyw = makeRailwayPlatformManageService.findBySpec(MakeRailwayPlatformManageParamyw);
    		if(manageResultyw != null) {
    			return ResponseData.error("已有进行中车辆");
    		}
    	}
    	this.makeRailwayPlatformManageService.update(makeRailwayPlatformManageParam);
    	return ResponseData.success();
    }
    
    
}
